package com.woniuxy.controller;

import com.woniuxy.entity.*;
import com.woniuxy.service.DetailService;
import com.woniuxy.service.UserService;
import com.woniuxy.util.Page;
import com.woniuxy.util.Result;
import com.woniuxy.util.onloadImgUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.*;

@RestController
@CrossOrigin
public class DetailController {

    @Autowired
    private UserService userService;
    @Autowired
    private DetailService detailService;

    @PostMapping("releaseDetails")
    public String releaseDetails(Communicate communicate
            , MultipartFile image0
            , MultipartFile image1
            , MultipartFile image2
            , MultipartFile image3
            , MultipartFile image4
            , MultipartFile image5
            , MultipartFile image6
            , MultipartFile image7
            , MultipartFile image8) throws Exception {

        MultipartFile[] image = new MultipartFile[9];
        image[0] = image0;
        image[1] = image1;
        image[2] = image2;
        image[3] = image3;
        image[4] = image4;
        image[5] = image5;
        image[6] = image6;
        image[7] = image7;
        image[8] = image8;

        ArrayList<Img> imgs = new ArrayList<>();




        for (MultipartFile f : image) {
            if(f == null) break;
            String imageName = f.getOriginalFilename();
            System.out.println(imageName);
            String suffix = imageName.substring(imageName.lastIndexOf("."));
            String prefix = UUID.randomUUID().toString().replace("-", "");
            imageName = prefix + suffix;

            onloadImgUtil.upload(imageName,f.getInputStream());

            String url = onloadImgUtil.fullPath(imageName);
            Img img = new Img();
            img.setUrl(url);
            img.setTypeId(6);
            imgs.add(img);
        }

        communicate.setPulishTime(new Date());
        communicate.setStatus(0);
        detailService.saveDetailAndImg(communicate,imgs);

        return "success";
    }

    @GetMapping("selectPosts")
    public Result selectPosts(Page page, Integer userId, Integer campId) throws Exception{
        int count = page.getCount();
        if(count == -1){
            count=detailService.queryCanSeeDetailCount(userId,campId);
            page.setCount(count);
            int pageCount = count % page.getNumber() == 0 ? count / page.getNumber() : count / page.getNumber() + 1;
            page.setPageCount(pageCount);
        }

        List<Map<String,Object>> list = detailService.queryCanSeeDetailLimit(page,userId,campId);

        return new Result("code","success",page,list);
    }

    @PostMapping("detail/islike")
    public String saveOrDeleteIsLike(PointLike pointLike, Integer islike) throws Exception{

        detailService.saveOrDeleteIsLike(pointLike,islike);

        return "suc";
    }
    @PostMapping("detial/content")
    public String saveDetialContent(CommunicateComment communicateComment, CommunicateContext communicateContext) throws Exception{

        detailService.saveDetialContent(communicateComment,communicateContext);

        return "suc";
    }
}